package mlz.d_field;

import java.lang.reflect.Field;

/*
    获取出来的成员变量的应用
 */
public class FieldDemo02 {
    public static void main(String[] args) throws NoSuchFieldException, InstantiationException, IllegalAccessException {
        //获取类的字节码文件
        Class<Student> c = Student.class;

        //获取名字字段
        Field name = c.getField("name");

        //利用set方法进行做赋值操作
        Student stu = c.newInstance();
        name.set(stu,"lucy");
        System.out.println(stu.name);
        System.out.println("--------------------");

        //获取私有的成员变量
        Field money = c.getDeclaredField("money");
        //取消一下访问检测
        money.setAccessible(true);
        //调用get方法进行获取值
        Student stu1 = c.newInstance();
        Object o = money.get(stu1);
        System.out.println(o);//1000




      /*  Student stu = c.newInstance();
        stu.name = "张三";*/



    }
}
